﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel;
using Excel.Log;
using FreeBitExcel.service;
using FreeBitExcel.service.@interface;
using FreeBitExcel.views;
using log4net.Core;
using ILog = log4net.ILog;

namespace FreeBitExcel
{
    public partial class MainForm : Form
    {
        public ILog Logger => this.GetLogger();
//        public ExcelService Service = new ExcelService();
        private readonly IExcelManager manager = new ExcelManager();


        public MainForm()
        {
            InitializeComponent();
            sheets.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.GetLogger().Info("界面加载完毕");
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            Logger.Debug("clean resourece");
            manager.Dispose();
            base.OnFormClosing(e);
        }

        private async void selectBtn_Click(object sender, EventArgs e)
        {
//初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                Logger.Info("file:" + fileDialog.FileName);
                //获取用户选择文件的后缀名
                string extension = Path.GetExtension(fileDialog.FileName);
                string[] exs = new string[] {".xlsx", ".xls"};
                if (!exs.Contains(extension))
                {
                    MessageBox.Show("仅能处理xlsx,xls的文件");
                    Logger.Warn("仅能处理xlsx, xls的文件");
                    return;
                }
                selectFile.Text = fileDialog.FileName;
                try
                {
                    //                    IExcelDataReader reader = null;
                    //                    FileStream stream = File.Open(fileDialog.FileName, FileMode.Open, FileAccess.Read);
                    //                    if (extension.Contains(exs[0]))
                    //                    {
                    //                        reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                    //                    }
                    //                    else
                    //                    {
                    //                        reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                    //                    }
                    //                    using (reader)
                    //                    {
                    //                        Logger.Info("读取excel : " + reader + "成功");
                    //                        var asDataSet = reader.AsDataSet();
                    //                        Service.DataSet = asDataSet;
                    //                        // op
                    //                        sheets.DataSource = Service.GetTablenames();
                    //                        sheets.SelectedIndex = 1;
                    //                    }
                    Logger.Info("开始读取excel sheets");
                    var names = await Task.Run(() => manager.LoadWorkSheets(selectFile.Text));
                    sheets.DataSource = names;
                    sheets.SelectedIndex = 1;
                    Logger.Info("读取sheets完毕");
                }
                catch (Exception exception)
                {
                    selectFile.Text = "";
                    MessageBox.Show(exception.Message);
                    return;
                }
            }
        }

        private void sheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            var box = sender as ComboBox;
            Logger.Info($"选择第{box?.SelectedIndex}项");
            if (box?.SelectedIndex != 0)
            {
                start.Visible = true;
                seeExcel.Visible = true;
            }
        }

        private async void start_Click(object sender, EventArgs e)
        {
            Logger.Info("开始处理数据");
//            Service.startExce(sheets.SelectedItem.ToString());
            manager.ReadValuse(sheets.SelectedItem.ToString());
        }

        public Action<DataSet> LoadDataAction { get; set; }

        private void seeExcel_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelContentForm objfrm = new ExcelContentForm();
//                objfrm.DataSet = Service.DataSet;
//                objfrm.TabName = sheets.SelectedItem.ToString();
                objfrm.Show();
            }
            catch (Exception exception)
            {
                Logger.Error("异常" + exception.Message);
            }
        }
    }
}